﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.Admin.monthsked
{
    public partial class Listmonth : Web.Admin.BasePage
    {
        Maticsoft.BLL.monthSupplayPlan bll = new Maticsoft.BLL.monthSupplayPlan();
        BLL.configinfo bc = new BLL.configinfo();
        public string dogid = "NO";
        public string Decode(string str)
        {

            if (dogid == "NO")
            {
                if (Request.Cookies["dogid"]!=null)
                    dogid = Request.Cookies["dogid"].Value.ToString();
            }
            return DecodeDog(str, dogid);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BLL.ruletype bll = new BLL.ruletype();
                DataSet ds = bll.GetAllList();
                ddltype.DataSource = ds.Tables[0];
                ddltype.DataTextField = "rtname";
                ddltype.DataValueField = "rtid";
                ddltype.DataBind();
                ddltype_SelectedIndexChanged(null, null);
                //Model.configinfo c = bc.GetModel();
                //lblmsg.Text = c.psbz.Value.ToString();
                //int nums = bll.GetRecordCount(" user_id is null ");
                //if (DateTime.Now.Day > c.psbz.Value && nums > 0)
                //    lblmsg.Text += "分配日期已过，还有未分配的任务请及时分配！";
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        #region gridView

        public void BindData()
        {


            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and sname like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtKeyword2.Text.Trim() != "")
            {
                if (Decode("1") == "1")
                strWhere.AppendFormat(" and projectname like '%{0}%'", txtKeyword2.Text.Trim());
            }
            if (txtwinbid.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and winbid like '%{0}%'", txtwinbid.Text.Trim());
            }
            strWhere.Append(" and arrivalAddress is not null order by sname");
            ds = bll.GetListnums(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl_isGoodsQualityStatus = e.Row.FindControl("lbl_isGoodsQualityStatus") as Label;

                Label lbl_goodsQualityStatus = e.Row.FindControl("lbl_goodsQualityStatus") as Label;

                CheckBox cb = e.Row.FindControl("DeleteThis") as CheckBox;

                if (lbl_isGoodsQualityStatus.Text == "False")
                {
                    lbl_isGoodsQualityStatus.Text = "否";
                    cb.Enabled = true;
                }
                else
                {
                    lbl_isGoodsQualityStatus.Text = "是";
                    if (!string.IsNullOrEmpty(lbl_goodsQualityStatus.Text))
                    {
                        cb.Enabled = true;
                    }
                    else
                    {
                        cb.Enabled = false;
                    }

                }
                Label l = e.Row.FindControl("lblsum") as Label;
                if (l != null)
                {
                    if (l.Text.Trim() != "")
                    {
                        int ssum = 0;
                        int.TryParse(l.Text, out ssum);
                        if (ssum >= 90)
                        {
                            e.Row.Attributes.Add("class", "rone");
                        }
                        else if (ssum >= 80)
                        {
                            e.Row.Attributes.Add("class", "rtwo");
                        }
                        else if (ssum >= 70)
                        {
                            e.Row.Attributes.Add("class", "rthree");
                        }
                        else
                        {
                            e.Row.Attributes.Add("class", "rfour");
                        }
                    }
                }

            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            string[] ids = idlist.Split(',');
            lcs.BLL.monthsked bllm = new lcs.BLL.monthsked();
            foreach (string id in ids)
            {
                int monthsupplyid = int.Parse(id);
                string spotunit = this.txtspotunit.Text;
                string spottype = this.txtspottype.Text;
                decimal spotnum = decimal.Parse(this.txtspotnum.Text);
                DataSet ds = bll.GetListnums("and monthSupplyPlanId="+id);

                decimal msnum = 0;// decimal.Parse(this.txtmsnum.Text);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    decimal.TryParse(ds.Tables[0].Rows[0]["thisMonthCount"].ToString() , out msnum);
                }
                    string bh = txtBH.Text;
                if (bllm.ExistsBH(bh))
                {
                    Maticsoft.Common.MessageBox.Show(this, "计划编号已存在请重新输入!");
                    return;
                }
                
                string spotcontent = "";
                string selval = "";
                for (int i = 0; i < cblrule.Items.Count; i++)
                {
                    if (cblrule.Items[i].Selected)
                    {
                        selval += cblrule.Items[i].Value + ",";
                    }
                }
                if (selval != "") selval = selval.Substring(0, selval.Length - 1);
                DateTime plandate = DateTime.Parse(this.txtplandate.Text);
                plandate = plandate.AddHours(8).AddMinutes(30);
                lcs.Model.monthsked model = new lcs.Model.monthsked();
                model.spotunit = spotunit;
                model.spottype = spottype;
                model.spotnum = spotnum;
                model.spotcontent = spotcontent;
                model.plandate = plandate;
                model.monthsupplyid = monthsupplyid;
                model.msBH = bh;
                model.msnum = msnum;
                model.mstatus = 0;
                bllm.Add(model,selval);
                BLL.gradeinfo bllg = new BLL.gradeinfo();
                bllg.Add(monthsupplyid);
            }
            Maticsoft.Common.MessageBox.Show(this, "抽检成功!");
        }

        protected void ddltype_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = 0;
            int.TryParse(ddltype.SelectedValue, out id);
            BLL.ruleinfo rbll = new BLL.ruleinfo();
            DataSet ds = rbll.GetList(" rtid=" + id);
            cblrule.DataSource = ds.Tables[0];
            cblrule.DataTextField = "rname";
            cblrule.DataValueField = "rid";
            cblrule.DataBind();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < cblrule.Items.Count; j++)
                {
                    if (this.cblrule.Items[j].Value == ds.Tables[0].Rows[i]["rid"].ToString())
                    {
                        if (ds.Tables[0].Rows[i]["ischecked"] != null)
                        {
                            bool isc = false;
                            bool.TryParse(ds.Tables[0].Rows[i]["ischecked"].ToString(), out isc);
                            this.cblrule.Items[j].Selected = isc;
                        }
                        break;
                    }
                    
                }
            }
        }
    }
}